package com.inputabc.ct.v1.app;

import com.formdev.flatlaf.FlatLightLaf;
import com.inputabc.ct.v1.context.ContextParams;
import com.inputabc.ct.v1.listener.SystemClipboardListener;
import com.inputabc.ct.v1.service.TranslationService;
import com.inputabc.ct.v1.service.impl.TranslationServiceImpl;
import com.inputabc.ct.v1.ui.TextBox;
import com.inputabc.ct.v1.ui.inputbox.InputBoxBuilder;
import com.inputabc.ct.v1.ui.keysetting.KeySettingBuilder;
import com.inputabc.ct.v1.util.DeepSeekUtils;
import com.inputabc.ct.v1.util.MessageBox;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author gaoweiyi
 * @version 1.9.3
 * @date 2025/4/20
 */
public class App {

    public static void main(String[] args) {
        try {
            // 移除无头模式设置，因为这是一个GUI应用
            // System.setProperty("java.awt.headless", "true");
            
            // 初始化UI外观
            try {
                UIManager.setLookAndFeel(new FlatLightLaf());
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            // 给剪切板注册监听器
            SystemClipboardListener systemClipboardListener = new SystemClipboardListener();
            // 初始化
            ContextParams.contextParam.put("systemClipboardListener", systemClipboardListener);
            
            // 初始化InputBoxBuilder
            InputBoxBuilder inputBoxBuilder = new InputBoxBuilder();
            ContextParams.contextParam.put("inputBoxBuilder", inputBoxBuilder);
            
            // 初始化KeySettingBuilder
            KeySettingBuilder keySettingBuilder = new KeySettingBuilder();
            ContextParams.contextParam.put("keySettingBuilder", keySettingBuilder);
            
            // 创建翻译服务实例
            TranslationService translationService = new TranslationServiceImpl();
            ContextParams.contextParam.put("translationService", translationService);

            // 创建TextBox实例
            TextBox textBox = new TextBox();
            // 将TextBox实例添加到上下文中，以便在其他地方可以获取到它
            ContextParams.contextParam.put("textBox", textBox);
            
            // 添加窗口关闭监听器，确保资源正确释放
            JFrame mainFrame = (JFrame) ContextParams.contextParam.get("textBoxFrame");
            if (mainFrame != null) {
                mainFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        cleanupResources();
                    }
                });
            }
            
            // 注册JVM关闭钩子，确保资源正确释放
            Runtime.getRuntime().addShutdownHook(new Thread(App::cleanupResources));

        } catch (Exception e) {
            e.printStackTrace();
            MessageBox.alert(e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * 清理应用程序资源
     */
    private static void cleanupResources() {
        try {
            System.out.println("正在关闭应用程序，清理资源...");
            
            // 关闭翻译服务线程池
            DeepSeekUtils.shutdown();
            
            // 清理翻译缓存
            if (ContextParams.contextParam.get("translationService") instanceof TranslationServiceImpl) {
                ((TranslationServiceImpl) ContextParams.contextParam.get("translationService")).clearAllCaches();
            }
            
            // 其他资源清理...
            System.out.println("资源清理完成");
        } catch (Exception e) {
            System.err.println("清理资源时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
